package cn.ecsd.action;

import java.util.Map;

import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.ecsd.entity.SysEmployee;
import cn.ecsd.service.EmployeeService;

//@Controller
public class LoginAction extends ActionSupport {
	// @Autowired
	// @Qualifier(value = "employeeService")
	private EmployeeService empService;

	public EmployeeService getEmpService() {
		return empService;
	}

	public void setEmpService(EmployeeService empService) {
		this.empService = empService;
	}

	private String random;

	public String getRandom() {
		return random;
	}

	public void setRandom(String random) {
		this.random = random;
	}

	private SysEmployee employee;

	public SysEmployee getEmployee() {
		return employee;
	}

	public void setEmployee(SysEmployee employee) {
		this.employee = employee;
	}

	@Override
	public String execute() throws Exception {
		SysEmployee emp = null;
		Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		Map<String, Object> session = (Map<String, Object>) ActionContext.getContext().getSession();
		emp = empService.empLogin(employee);
		if (session.get("checkCode").equals(random)) {
			if (emp != null) {
				if (employee.getPassword().equals(emp.getPassword())) {
					session.put("employee", emp);
					session.put("employee_position", emp.getSysPosition().getNameCn());
					return SUCCESS;
				} else {
					request.put("msg", "您输入的密码有误");
				}
			} else {
				// 用户不存在
				request.put("msg", "该工号不存在");
			}
		} else {
			request.put("msg", "验证码不正确");
		}
		return ERROR;
	}
}
